package erfgame.core.world.generator.terrain;

import erfgame.core.world.terrain.TerrainConstants;

public class CylinderTerrainBlockGenerator implements TerrainBlockGenerator {

	private int cx;
	private int cy;
	private int z;
	private int r;
	private int d;
	private byte terrainType;
	
	public CylinderTerrainBlockGenerator( int cx, int cy, int r, int z, int d, byte terrainType ) {
		this.cx = cx;
		this.cy = cy;
		this.r = r;
		this.z = z;
		this.d = d;
		this.terrainType = terrainType;
	}
	
	public boolean generate(int bx, int by, int bz, int bw, int bh, int bd,
			byte[][][] terrain) {
		int txoff = bx * bw;
		int tyoff = by * bh;
		int tzoff = bz * bd;
		int rsq = r*r;
		for( int tx=bw; tx>0; ) {
			tx--;
			for( int ty=bh; ty>0; ) {
				ty--;
				for( int tz=bd; tz>0; ) {
					tz--;
					byte t;
					int dx = cx - (tx+txoff);
					int dy = cy - (ty+tyoff);
					if(dx*dx + dy*dy <= rsq) {
						if( tz+tzoff >= z && tz+tzoff < z + d ) {
							t = terrainType;
						} else {
							t = TerrainConstants.TERRAIN_TYPE_AIR;
						}
					} else {
						t = TerrainConstants.TERRAIN_TYPE_AIR;
					}
					terrain[tx][ty][tz] = t;
				}
			}
		}
		return true;
	}

}
